package com.jrzh.security.authority;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
/**
 * 权限配置资源管理器
 * @author Xanthin
 *
 */
public class CustomSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
	
	private static final Logger logger = Logger.getLogger(CustomSecurityMetadataSource.class);
	private Map<String, Collection<ConfigAttribute>> resourceMap = null;
	private PathMatcher pathMatcher = new AntPathMatcher();
	private String urlroles;
	
	public CustomSecurityMetadataSource(String urlroles) {
		super();
		this.urlroles = urlroles;
		resourceMap = loadResourceMatchAuthority();
	}
	
	private Map<String, Collection<ConfigAttribute>> loadResourceMatchAuthority() {
		Map<String, Collection<ConfigAttribute>> map = new HashMap<String, Collection<ConfigAttribute>>();
		if(urlroles != null && !urlroles.isEmpty()){
			String[] resources = urlroles.split(";");
			for(String resource : resources){
				String[] urls = resource.split("=");
				String[] roles = resource.split(",");
				Collection<ConfigAttribute> list = new ArrayList<ConfigAttribute>();
				for(String role : roles){
					ConfigAttribute config = new SecurityConfig(role.trim());
					list.add(config);
				}
				 //key：url, value：roles
				map.put(urls[0].trim(), list);
			}
		}else{
			logger.error("'securityconfig.urlroles' must be set");
		}
		logger.info("Loaded UrlRoles Resources.");
		return map;
	}
	
	@Override
	public Collection<ConfigAttribute> getAllConfigAttributes() {
		return null;
	}

	@Override
	public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
		String url = ((FilterInvocation)object).getRequestUrl();
		logger.debug("request url is  " + url);
		if(resourceMap == null)resourceMap = loadResourceMatchAuthority();
		Iterator<String> ite = resourceMap.keySet().iterator();
		while(ite.hasNext()){
			String resURL = ite.next();
			if(pathMatcher.match(resURL, url)){
				return resourceMap.get(resURL);
			}
		}
		return resourceMap.get(url);
	}

	@Override
	public boolean supports(Class<?> arg0) {
		return true;
	}

}
